home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / fontutil.6 / fontutil / fontutils-0.6 / imageto / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-24  |  12.8 KB  |  415 lines

  1. /* imageto -- convert a scanned image.
  2.  
  3. Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include <ctype.h>
  22.  
  23. #define CMDLINE_NO_DPI /* It's not in the input filename.  */
  24. #include "cmdline.h"
  25. #include "encoding.h" /* For `DEFAULT_ENCODING'.  */
  26. #include "getopt.h"
  27. #include "gf.h"
  28. #include "report.h"
  29.  
  30. #include "ifi.h" /* For `encoding_name'.  */
  31. #include "image-header.h"
  32. #include "input-img.h"
  33. #include "input-pbm.h"
  34. #include "main.h"
  35. #include "out-chars.h"
  36. #include "out-epsf.h"
  37. #include "out-strips.h"
  38.  
  39.  
  40. /* General information about the image.  Set by the input routines.  */
  41. image_header_type image_header;
  42.  
  43. /* Pointers to functions based on input format.  (-input-format)  */
  44. void (*image_open_input_file) (string) = NULL;
  45. void (*image_close_input_file) (void) = NULL;
  46. void (*image_get_header) (void) = NULL;
  47. boolean (*image_get_scanline) (one_byte *) = NULL;
  48.  
  49. /* The filename for the input image.  */
  50. string input_name;
  51.  
  52. /* Show every scanline on the terminal as we read it?  (-trace-scanlines) */
  53. boolean trace_scanlines = false;
  54.  
  55. /* Private variables.  */
  56.  
  57. /* The design size of the font we're creating.  (-designsize)  */
  58. static real design_size = 10.0;
  59.  
  60. /* The suffix for the image file.  */
  61. static string input_extension;
  62.  
  63. /* If set, write an EPSF file.  (-epsf)  */
  64. static boolean output_epsf = false;
  65.  
  66. /* The name of the file we're going to write.  (-output-file) */
  67. static string output_name;
  68.  
  69. /* If set, make each ``character'' a (more or less) constant number of
  70.    scanlines.  (-strips) */
  71. static boolean do_strips = false;
  72.  
  73.  
  74. static string read_command_line (int, string[]);
  75. static void set_img_input_format (void);
  76. static void set_input_format (string);
  77. static void set_pbm_input_format (void);
  78.  
  79. /* We have three different strategies for processing the image:
  80.      1)  (normal) analyze the image and write out the ``true'' characters,
  81.      2) (-strips) takes a constant number of scanlines as each character,
  82.      3)   (-epsf) write out the image as an Encapsulated PostScript file.
  83.    
  84.    The output name is, correspondingly, constructed differently:
  85.      1) if `output_name' was supplied, and has a suffix, that's it.
  86.      2) if `output_name' was supplied, but has no suffix, append `.eps'
  87.         if we're doing EPSF, else append `<suffix>.<dpi>gf', where
  88.         <suffix> is 
  89.           a) `sp', if we are doing strips;
  90.           b) the design size, if not.
  91.      3) if `output_name' was not supplied, use the basename of the input
  92.         filename extended as in #2.
  93.    */
  94.  
  95. int
  96. main (int argc, string argv[])
  97. {
  98.   boolean writing_gf;
  99.  
  100.   input_name = read_command_line (argc, argv);
  101.   writing_gf = !output_epsf;
  102.   
  103.   set_input_format (input_name);  
  104.  
  105.   /* If the input format is PBM, then they must also tell us the dpi.  */
  106.   if (image_open_input_file == pbm_open_input_file 
  107.       && image_header.hres == 0)
  108.     {
  109.       fprintf (stderr, "If you use PBM format, you must supply the dpi.\n");
  110.       fprintf (stderr, "For more information, use ``-help''.\n");
  111.       exit (1);
  112.     }
  113.  
  114.   /* Open the main input file.  */
  115.   (*image_open_input_file) (extend_filename (input_name, input_extension));
  116.   
  117.   /* We need the horizontal resolution before we can make the GF name,
  118.      so, at least for IMG input, have to read the header.  */
  119.   (*image_get_header) ();
  120.  
  121.   /* If the user didn't give an output name, use the input name.  */
  122.   if (output_name == NULL)
  123.     output_name = remove_suffix (basename (input_name));
  124.   
  125.   /* If they didn't give an output name with a suffix, use ours.  */
  126.   if (find_suffix (output_name) == NULL)
  127.     {
  128.       if (writing_gf)
  129.         {
  130.           char dpi[MAX_INT_LENGTH + 1];
  131.           string suffix = do_strips ? "sp" : itoa ((unsigned) design_size);
  132.  
  133.           sprintf (dpi, "%u", image_header.hres);
  134.           output_name = concat5 (output_name, suffix, ".", dpi, "gf");
  135.     }
  136.       else if (output_epsf)
  137.         output_name = concat (output_name, ".eps");
  138.       else
  139.         abort (); /* Should never happen.  */
  140.     }
  141.  
  142.   /* If necessary, open the GF file.  */
  143.   if (writing_gf)
  144.     {
  145.       if (!gf_open_output_file (output_name))
  146.         FATAL_PERROR (output_name);
  147.  
  148.       /* Identify ourselves in the GF comment.  */
  149.       gf_put_preamble (concat ("imageto output ", now () + 4));
  150.     }
  151.  
  152.   /* Do the real work, whichever the user wants.  */
  153.   if (do_strips)
  154.     { /* The design size is irrelevant when we're creating strips,
  155.          but it should be pretty large, lest the relative dimensions in
  156.          the TFM file get too big.  */
  157.       design_size = 100.0;
  158.       write_chars_as_strips (image_header, design_size);
  159.     }
  160.   else if (output_epsf)
  161.     write_epsf (output_name, image_header);
  162.   else
  163.     write_image_chars (image_header, design_size);
  164.  
  165.   /* Clean up.  */
  166.   if (verbose)
  167.     REPORT ("\n");
  168.  
  169.   if (writing_gf)
  170.     {
  171.       /* We've read all the characters we're supposed to (or else the whole
  172.          image).  Finish up the font.  */
  173.       gf_put_postamble (real_to_fix (design_size),
  174.                         (real) image_header.hres, (real) image_header.vres);
  175.       gf_close_output_file ();
  176.     }
  177.  
  178.   (*image_close_input_file) ();
  179.   
  180.   return 0;
  181. }
  182.  
  183. /* We are semi-clever about printing this, for the sake of huge images.  */
  184.  
  185. void
  186. print_scanline (one_byte line[], unsigned width)
  187. {
  188.   static unsigned scanline_count = 0;
  189.  
  190.   if (trace_scanlines)
  191.     {
  192.       printf ("%7d:", scanline_count++);
  193.       
  194.       /* If the line is entirely white, don't print anything.  */
  195.       if (memchr (line, BLACK, width))
  196.         {
  197.           unsigned c;
  198.           
  199.           for (c = 0; c < width; c++)
  200.             { /* Compress eight consecutive spaces to a tab, if we're at
  201.                  the beginning of a tab mark.  This handles the usual
  202.                  case, although we could do still better.  */
  203.               if (c % 8 == 0 && c + 7 < width
  204.               && memchr (line + c, BLACK, 8) == NULL)
  205.                 {
  206.                   putchar ('\t');
  207.                   c += 7;
  208.         }
  209.               else
  210.                 putchar (line[c] ? '*' : ' ');
  211.         }
  212.     }
  213.  
  214.       putchar ('\n');
  215.     }
  216. }
  217.  
  218. /* Reading the options.  */
  219.  
  220. /* This is defined in version.c.  */
  221. extern string version_string;
  222.  
  223. #define USAGE "Options:
  224. <font_name> should be a base filename, e.g., `ggmr'.  (More properly, it
  225. is an <image_name>, not a <font_name>.)"                 \
  226.   GETOPT_USAGE                                \
  227. "baselines <row1>,<row2>,...: define the baselines for each image row.
  228.   The baseline of the first image row is taken to be scanline <row1>, etc.
  229. designsize <real>: set the designsize of the font to <real>; default is 10.0.
  230. dpi <unsigned>: resolution (required for pbm input).
  231. encoding <filename>: read ligature and other encoding information
  232.   from `<filename>.enc'; the default is to assign successive character codes.
  233. epsf: write the image as an Encapsulated PostScript file, instead of a
  234.   bitmap font.
  235. help: print this message.
  236. ifi-file <filename>: use <filename>.ifi (if <filename doesn't have a
  237.   suffix; otherwise use <filename>) for the IFI filename; default is
  238.   `<font_name>.ifi'.
  239. input-format <format>: specify format of input image; <format> must be
  240.   one of `pbm' or `img'.
  241. nchars <unsigned>: only write the first <unsigned> (approximately)
  242.   characters to the font; default is infinity.
  243. output-file <filename>: write to <filename> if <filename> has a suffix.
  244.   If <filename> doesn't have a suffix, then if writing strips, write to
  245.   <filename>sp.<dpi>gf and to <filename>.<dpi>gf if not.  By default,
  246.   use <font_name> for <filename>.
  247. print-clean-info: print gray values for the bounding boxes that are
  248.   considered for cleaning.  This implies `-verbose'.
  249. print-guidelines: print the numbers of the top and bottom rows (in that
  250.   order) of each character.  This implies `-verbose'.
  251. range <char1>-<char2>: only process characters between <char1> and
  252.   <char2>, inclusive. 
  253. strips: take a constant number of scanlines as each character,
  254.   instead of using an IFI file to analyze the image.
  255. trace-scanlines: show every scanline as we read it.
  256. verbose: output progress reports.
  257. version: print the version number of this program.
  258. "
  259.  
  260. static string
  261. read_command_line (int argc, string argv[])
  262. {
  263.   int g;  /* `getopt' return code.  */
  264.   int option_index;
  265.   boolean printed_version = false;
  266.   struct option long_options[]
  267.     = { { "baselines",        1, 0, 0 },
  268.         { "designsize",        1, 0, 0 },
  269.         { "encoding",        1, 0, 0 },
  270.         { "epsf",        0, (int *) &output_epsf, 1 },
  271.         { "help",               0, 0, 0 },
  272.     { "dpi",        1, 0, 0 },
  273.         { "ifi-file",        1, 0, 0 },
  274.         { "nchars",        1, 0, 0 },
  275.         { "input-format",    1, 0, 0 },
  276.         { "output-file",    1, 0, 0 },
  277.         { "print-clean-info",    0, (int *) &print_clean_info, 1 },
  278.         { "print-guidelines",    0, (int *) &print_guidelines, 1 },
  279.         { "range",              1, 0, 0 },
  280.         { "strips",        0, (int *) &do_strips, 1 },
  281.         { "trace-scanlines",    0, (int *) &trace_scanlines, 1 },
  282.         { "verbose",        0, (int *) &verbose, 1 },
  283.         { "version",            0, (int *) &printed_version, 1 },
  284.         { 0, 0, 0, 0 } };
  285.  
  286.   while (true)
  287.     {
  288.       g = getopt_long_only (argc, argv, "", long_options, &option_index);
  289.       
  290.       if (g == EOF)
  291.         break;
  292.  
  293.       if (g == '?')
  294.         exit (1);  /* Unknown option.  */
  295.   
  296.       assert (g == 0); /* We have no short option names.  */
  297.       
  298.       if (ARGUMENT_IS ("baselines"))
  299.         baseline_list = scan_unsigned_list (optarg);
  300.       
  301.       else if (ARGUMENT_IS ("designsize"))
  302.         design_size = atof (optarg);
  303.       
  304.       else if (ARGUMENT_IS ("dpi"))
  305.     {
  306.           image_header.hres = (two_bytes) atou (optarg);
  307.           image_header.vres = image_header.hres;
  308.         }
  309.  
  310.       else if (ARGUMENT_IS ("encoding"))
  311.         encoding_name = optarg;
  312.  
  313.       else if (ARGUMENT_IS ("help"))
  314.         {
  315.           fprintf (stderr, "Usage: %s [options] <font_name>.\n", argv[0]);
  316.           fprintf (stderr, USAGE);
  317.           exit (0);
  318.         }
  319.  
  320.       else if (ARGUMENT_IS ("ifi-file"))
  321.         ifi_filename = optarg;
  322.  
  323.       else if (ARGUMENT_IS ("input-format"))
  324.     {
  325.       if (STREQ ("pbm", optarg))
  326.             set_pbm_input_format ();
  327.       else if (STREQ ("img", optarg))
  328.             set_img_input_format ();
  329.           else
  330.             FATAL1 ("imageto: Unknown input format `%s'; expected one of \
  331. `img' or `pbm'", optarg);
  332.         }
  333.  
  334.       else if (ARGUMENT_IS ("nchars"))
  335.         nchars_wanted = atou (optarg);
  336.  
  337.       else if (ARGUMENT_IS ("output-file"))
  338.         output_name = optarg;
  339.  
  340.       else if (ARGUMENT_IS ("print-clean-info")
  341.                || ARGUMENT_IS ("print-guidelines"))
  342.         verbose = true;
  343.  
  344.       else if (ARGUMENT_IS ("range"))
  345.         GET_RANGE (optarg, starting_char, ending_char);
  346.       
  347.       else if (ARGUMENT_IS ("version"))
  348.         printf ("%s.\n", version_string);
  349.  
  350.       /* Else it was a flag; getopt has already done the assignment.  */
  351.     }
  352.   
  353.   if (do_strips && output_epsf)
  354.     FATAL ("imageto: Sorry, -strips and -epsf are mutually exclusive");
  355.  
  356.   FINISH_COMMAND_LINE ();
  357. }
  358.  
  359. /* If the input format wasn't explicitly set on the command line,
  360.    attempt to intuit it from FILENAME, and set the necessary variables.
  361.    If we can't tell what the format should be, quit.  */
  362.  
  363. static void
  364. set_input_format (string filename)
  365. {
  366.   string input_extension;
  367.   
  368.   /* If it's already set, just return.  */
  369.   if (image_open_input_file != NULL)
  370.     return;
  371.  
  372.   /* Try to guess based on FILENAME.  */
  373.   input_extension = find_suffix (filename) ? : "";
  374.  
  375.   if (STREQ (input_extension, "img"))
  376.     set_img_input_format ();
  377.  
  378.   else if (STREQ (input_extension, "pbm"))
  379.     set_pbm_input_format ();
  380.   
  381.   else /* Can't guess it; quit.  */
  382.     {
  383.       fprintf (stderr, "You must supply an input format.\n");
  384.       fprintf (stderr, "(I can't guess from the filename `%s'.)\n", filename);
  385.       fprintf (stderr, "For more information, use ``-help''.\n");
  386.       exit (1);
  387.     }
  388. }
  389.  
  390.  
  391. /* Set up for reading a PBM file.  */
  392.  
  393. static void
  394. set_pbm_input_format ()
  395. {
  396.   image_open_input_file = pbm_open_input_file;
  397.   image_close_input_file = pbm_close_input_file;
  398.   image_get_header = pbm_get_header;
  399.   image_get_scanline = pbm_get_scanline;
  400.   input_extension = "pbm";
  401. }
  402.  
  403.  
  404. /* Set up for reading an IMG file.  */
  405.  
  406. static void
  407. set_img_input_format ()
  408. {
  409.   image_open_input_file = img_open_input_file;
  410.   image_close_input_file = img_close_input_file;
  411.   image_get_header = img_get_header;
  412.   image_get_scanline = img_get_scanline;
  413.   input_extension = "img";
  414. }
  415.